﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace QuantumQuest
{
    public partial class QQTestLevel : QQLevel
    {
        private DispatcherTimer _timer;
        private QQPhysics _phys;

        private Line _xAxis;
        private Line _yAxis;


        private SolidColorBrush _lineBrush;
        private SolidColorBrush _lineBrush2;

        private QQPlanet p1;
        private QQPlanet p2;
        private QQPlanet p3;

        private TextBlock t1;
        private TextBlock t2;
        private TextBlock t3;

        private QQList _textBlocks;

        private int _count=0;


        public QQTestLevel()
        {
            InitializeComponent();
            InitializeLevel();
            InitializeTestLevel();

        }
        
        public override void ShutDown()
        {

            //todo
        }
        public override void Begin()
        {
            _timer.Start();
        }
        public void InitializeTestLevel()
        {
            _timer = new DispatcherTimer();
            _timer.Interval = TimeSpan.FromSeconds(1.0 / (QQGlobal.FramerateSeconds));
            _timer.Tick += new EventHandler(_timer_Tick);

            _phys = new QQPhysics();


            _bodies = new QQList(4);
            p1 = new QQPlanet(1000000000000000,new QQVector2(150, 75));
            p2 = new QQPlanet(1000000000000000, new QQVector2(150, 175));
            p3 = new QQPlanet(1000000000000000, new QQVector2(150, 350));
            QQPlanetEarth earth = new QQPlanetEarth(1000000000000000, new QQVector2(10, 30));

            p1.VelocityVec = new QQVector2(10, -10);
            //p2.IsStationary = true;

            _textBlocks = new QQList(3);
            t1 = new TextBlock();
            t2 = new TextBlock();
            t3 = new TextBlock();

            _lineBrush = new SolidColorBrush();
            _lineBrush.Color = Colors.Cyan;


            _lineBrush2 = new SolidColorBrush();
            _lineBrush2.Color = Colors.Orange;

            _xAxis = new Line();
            _xAxis.StrokeThickness = 2;
            _xAxis.Stroke = _lineBrush;
            _yAxis = new Line();
            _yAxis.StrokeThickness = 2;
            _yAxis.Stroke = _lineBrush;

            _xAxis.X1 = 0;
            _xAxis.X2 = 20;
            _xAxis.Y1 = 0;
            _xAxis.Y2 = 0;

            _yAxis.X1 = 0;
            _yAxis.X2 = 0;
            _yAxis.Y1 = 0;
            _yAxis.Y2 = 20;

       
        

            TestLevelCanvas.Children.Add(_xAxis);
            TestLevelCanvas.Children.Add(_yAxis);
       

            _bodies.Add(p1);
            _bodies.Add(p2);
            _bodies.Add(p3);
            _bodies.Add(earth);

            _textBlocks.Add(t1);
            _textBlocks.Add(t2);
            _textBlocks.Add(t3);
            /*t1.SetValue(Canvas.TopProperty, 0.0);
            t2.SetValue(Canvas.TopProperty, 75.0);
            t3.SetValue(Canvas.TopProperty, 150.0);

            t1.SetValue(Canvas.LeftProperty, 100.0);
            t2.SetValue(Canvas.LeftProperty, 300.0);
            t3.SetValue(Canvas.LeftProperty, 400.0);*/

            for (int i = 0; i < _bodies.Count; i++)
            {
                TestLevelCanvas.Children.Add((QQBody)_bodies[i]);
                //((TextBlock)_textBlocks[i]).Text = "TextBlock " + Convert.ToString(i);
                //TestLevelCanvas.Children.Add((TextBlock)_textBlocks[i]);
            }
        }
        void _timer_Tick(object sender, EventArgs e)
        {

            for (int i = 0; i < _bodies.Count; i++)
            {
                ((QQBody)(_bodies[i])).DynamicsParameters = (_phys.ApplyGravity2(_bodies, i, (1.0) / (QQGlobal.FramerateSeconds))).DynamicsParameters;
                //((TextBlock)(_textBlocks[i])).FontSize = 8;
                //((TextBlock)(_textBlocks[i])).Text = "Planet " + Convert.ToString(i) + ":\n" + ((QQPlanet)(_bodies[i])).DynamicsParameters;
               
                //((TextBlock)(_textBlocks[i])).SetValue(Canvas.TopProperty, (double)((QQPlanet)(_bodies[i])).PositionVec.Y + 15.0);
                //((TextBlock)(_textBlocks[i])).SetValue(Canvas.LeftProperty, (double)((QQPlanet)(_bodies[i])).PositionVec.X + 15.0);
               
                //((TextBlock)(_textBlocks[i])).Visibility = Visibility.Collapsed;
                _phys.UpdateCollisions3(ref _bodies);
               
                   
             


                if (_count % 30 == 0)
                {
                    //SolidColorBrush pathB = new SolidColorBrush();
                    //pathB.Color = Colors.Orange;

                    //Line path = new Line();
                    //path.StrokeThickness = 1;


                    //path.Stroke = pathB;
                    //path.X1 = ((QQPlanet)(_bodies[i])).PositionVec.X;
                    //path.X2 = ((QQPlanet)(_bodies[i])).PositionVec.X + ((QQPlanet)(_bodies[i])).DynamicsParameters.Velocity.X;
                    //path.Y1 = ((QQPlanet)(_bodies[i])).PositionVec.Y;
                    //path.Y2 = ((QQPlanet)(_bodies[i])).PositionVec.Y + ((QQPlanet)(_bodies[i])).DynamicsParameters.Velocity.Y;

                    //TestLevelCanvas.Children.Add(path);
                }
                _count++;



                
            }
        }

    }
}
